<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if='detail'>
      <!-- 面包屑 -->
      <div class="break-crumb">
        <span>首页 > </span>
        <span>{{ detail.categories[1].name }} ></span>
        <span>{{ detail.categories[0].name }} ></span>
        <span>{{ detail.name}}</span>
      </div>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <goods-image :images='detail.mainPictures' />
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :detail="detail" />
          <GoodsSku @sku-info="getskuInfo" :specs="detail.specs" :skus='detail.skus' />
          <!-- 商品数量控制 -->
          <XtxNumbox v-model="count" :max='detail.inventory'>数量</XtxNumbox>
          <XtxButton @click='addCart' style='margin-top: 10px' size='middle' type='primary'>加入购物车</XtxButton>

        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"></div>
          <!-- 注意事项 -->
          <div class="goods-warn"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { findGoods } from '@/api/product.js'
// 导入vue3中的方法，用来获取路由中的id
import { useRoute } from 'vue-router'
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'

const useGoods = () => {
  const route = useRoute()
  const detail = ref(null)

  // 获取商品详情数据(动态路由默认是有缓存的)
  findGoods(route.params.id).then(ret => {
    detail.value = ret.result
  })

  return detail
}

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku },
  setup () {
    const detail = useGoods()

    // 获取sku信息
    const getskuInfo = (info) => {
      console.log(info)
      if (info) {
        detail.value.oldPrice = info.oldPrice
        detail.value.price = info.price
        detail.value.inventory = info.inventory
      }
    }

    // 购买商品的数量
    const count = ref(1)

    return { detail, count, getskuInfo }
  }
}
</script>

<style scoped lang='less'>
.break-crumb {
  padding: 25px 0;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
